﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Drawing;
using System.Collections;

namespace gpsmid_wince {
	class MapLayerXml : MapLayer {
		private XmlDocument doc;
		Dictionary<String, XmlNode> nodes = new Dictionary<String, XmlNode>();
		struct Way {public XmlNode xmlnode; public List<XmlNode> nodes; }
		List<Way> ways = new List<Way>();

		public MapLayerXml(String file) {
			XmlTextReader xrd = new XmlTextReader(file);
			xrd.WhitespaceHandling = WhitespaceHandling.None;
			doc = new XmlDocument();
			doc.Load(xrd);
			XmlNodeList list = doc.DocumentElement.ChildNodes;
			foreach(XmlNode node in list) {
				//XmlNode node = list.Item(i);
				if(node.Name == "node") nodes.Add(node.Attributes["id"].Value, node);
				if(node.Name == "way") ways.Add(new Way(){xmlnode=node,nodes=new List<XmlNode>()});
			}
			foreach(Way way in ways) {
				XmlNodeList ll = way.xmlnode.ChildNodes;
				foreach(XmlNode nn in ll) if(nn.Name=="nd") way.nodes.Add( nodes[nn.Attributes["ref"].Value]);
			}
		}

		private float parse(String val) {
			//Console.Write(System.Globalization.NumberFormatInfo.InvariantInfo.NumberDecimalSeparator);
			return float.Parse(val, System.Globalization.NumberFormatInfo.InvariantInfo  );
		}

		override public void render(Graphics g) {
			List<Point> pts = new List<Point>();
			foreach(Way way in ways) {
				pts.Clear();
				foreach(XmlNode node in way.nodes) pts.Add(
					cProj.toWin(parse(node.Attributes["lat"].Value)/180*(float)Math.PI, parse(node.Attributes["lon"].Value)/180*(float)Math.PI ));
				g.DrawLines(cPen, pts.ToArray());
			}

		}
		private Pen cPen = new Pen(Color.LightGray);


	}
}
